
#include "car_road_info_manager.h"

car_road_info_manager::car_road_info_manager(const char* file_name) :
    m_is_read_file(false) {
    m_is_read_file = bind_file(file_name) ;
    
}

bool car_road_info_manager::bind_file(const char* file_name) {
    return  m_nodeMgr.read_from_file(file_name) ;
}

void car_road_info_manager::move_to_begin(int id) {
    assert(is_valid(id)) ;
    m_id_roadpoint[id] = 0 ;
}

void car_road_info_manager::move_to_end(int id) {
    assert(is_valid(id));
    m_id_roadpoint[id] = m_nodeMgr.size() -1 ;
}


void car_road_info_manager::move_to_next(int id) {
    assert(is_valid(id)) ;
    ++m_id_roadpoint[id] ;
}

void car_road_info_manager::move_to_prev(int id) {
    assert(is_valid(id)) ;
    --m_id_roadpoint[id] ;
}

bool car_road_info_manager::prev_is_valid(int id) const {
    return m_id_roadpoint[id] > 0 ;
}
bool car_road_info_manager::next_is_valid(int id) const {
    return m_id_roadpoint[id]  < static_cast<int>(m_nodeMgr.size()) -1 ;
}


bool car_road_info_manager::is_valid(int id) const {
    return (m_is_read_file && m_id_roadpoint.count(id) &&
            m_id_roadpoint[id] < m_nodeMgr.size() && m_id_roadpoint[id] >= 0) ;
}
const normal_number_range* car_road_info_manager::current_position(int id) const {
    if(!is_valid(id)) return 0 ;   
    return &boost::get<NODE_POSITION>(m_nodeMgr[m_id_roadpoint[id]]) ;
}

int car_road_info_manager::current_round(int id) const {
    if(!is_valid(id)) return 0 ;
    return boost::get<NODE_ROUND>(m_nodeMgr[m_id_roadpoint[id]]) ;
}

const normal_number_range* car_road_info_manager::next_pos(int id, int index) const {
    assert(index > 0) ;
    if(!is_valid(id)) return 0 ;
    if(static_cast<size_t>(m_id_roadpoint[id] + index) >= m_nodeMgr.size())
        return 0 ;
    return &boost::get<NODE_POSITION>(m_nodeMgr[m_id_roadpoint[id] + index]) ;
}

const normal_number_range* car_road_info_manager::prev_pos(int id, int index) const {
    assert(index > 0) ;
    if(!is_valid(id)) return 0 ;
    if(m_id_roadpoint[id] - index < 0) return 0 ;
    return &boost::get<NODE_POSITION>(m_nodeMgr[m_id_roadpoint[id] - index]) ;
}

float car_road_info_manager::current_x(int id) const {
    assert(is_valid(id)) ;
    return boost::get<NODE_X>(m_nodeMgr[m_id_roadpoint[id]]) ;
}

float car_road_info_manager::current_y(int id) const {
    assert(is_valid(id)) ;
    return boost::get<NODE_Y>(m_nodeMgr[m_id_roadpoint[id]]) ;
}

float car_road_info_manager::next_x(int id, int index) const {
    assert(index > 0 && is_valid(id) && m_id_roadpoint[id] + index < m_nodeMgr.size()) ;
    return boost::get<NODE_X>(m_nodeMgr[m_id_roadpoint[id]]) ;
}

float car_road_info_manager::next_y(int id, int index) const {
    assert(index > 0 && is_valid(id) && m_id_roadpoint[id] + index < m_nodeMgr.size()) ;
    return boost::get<NODE_Y>(m_nodeMgr[m_id_roadpoint[id]]) ;
}
    

void car_road_info_manager::reset(int id) {
    assert(is_valid(id)) ;
    m_id_roadpoint[id] = 0 ;
}

void car_road_info_manager::bind_car(int id) {
    if(!has_car_id(id)) m_id_roadpoint[id] = 0 ;
}

bool car_road_info_manager::has_car_id(int id) const{
    return m_id_roadpoint.count(id) != 0 ;
}
        




